package snu.bi.MSAgent;

import snu.bi.MSAgent.R;
import snu.bi.MSAgent.DB.MSADBHandler;

import java.util.TimeZone;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

// test

public final class valuepopup extends Dialog {
	EditText iv;
	OnDismissListener _listener;
	int kind;
	Button ok, cancel;
	//String dbpath;
	Context preContext;
	
	public valuepopup(Context context, int k) {
		super(context);
		kind=k;
		preContext = context;
		//dbpath = dpath;
		// TODO Auto-generated constructor stub
	}
	
	public void onCreate(Bundle $savedInstanceState) {
		super.onCreate($savedInstanceState);
		setContentView(R.layout.value_popup);
		
		TimeZone timezone = TimeZone.getTimeZone("Etc/GMT-9");
		TimeZone.setDefault(timezone);
		
		iv = (EditText)findViewById(R.id.popup_input_value);
		ok = (Button)findViewById(R.id.value_popup_ok);
		cancel = (Button)findViewById(R.id.value_popup_cancel);
		
		
		if(kind == 0) setTitle("Input your weight(kg)");			
		else if(kind == 1) setTitle("Input your height(cm)");			
		else if(kind == 2) setTitle("Input your Age(years)");			
		else if(kind == 3) setTitle("Input your sex(0:man, 1:woman)");
		else {
			setTitle("What is your Search Word?\nIf you input blank, then go back to category");
			iv.setInputType(InputType.TYPE_CLASS_TEXT);
			ok.setOnClickListener(new View.OnClickListener() {				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if( _listener == null ) {} else {  
	                    _listener.onDismiss( valuepopup.this ) ;  
	                }
					dismiss();
				}
			});
			cancel.setOnClickListener(new View.OnClickListener() {			
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if( _listener == null ) {} else {  
	                    _listener.onDismiss( valuepopup.this ) ;  
	                }
					iv.setText("");
					dismiss();
				}
			});
			return;
		}		
		
		ok.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//dismiss();
				
				MSADBHandler tempdb = MSADBHandler.open(preContext);
				String tablename = new String();
				String value = iv.getText().toString();
				
				if(kind == 0) tablename = "weight_table";
				else if(kind == 1) tablename = "height_table";
				else if(kind == 2) tablename = "age_table";
				else tablename = "sex_table";
				
				ContentValues newValues = new ContentValues();
				newValues.put("time", new java.util.Date().getTime());
				//String kkk = DateFormat.format("yyyy-MM-dd HH:mm:ss", new java.util.Date().getTime()).toString();
				newValues.put("value", value);
				tempdb.insert(tablename, null, newValues);
				tempdb.close();    			
				
				if(kind == 0 || kind == 1) calculateBMI(preContext);
				if( _listener == null ) {} else {  
                    _listener.onDismiss( valuepopup.this ) ;  
                }  
				dismiss();
			}
		});
		cancel.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if( _listener == null ) {} else {  
                    _listener.onDismiss( valuepopup.this ) ;  
                }  
				dismiss();
			}
		});
	}
	public void setOnDismissListener(OnDismissListener $listener) {
		_listener = $listener;
	}
	String getmaxtimeindb(MSADBHandler sdb, String tablename) {
		Cursor cur = sdb.rawQuery("select MAX(time) from "+tablename);
		cur.moveToFirst();
		String re = cur.getString(0);
		cur.close();
		return re;
	}	
	void calculateBMI(Context act) {
		double we=0;
		double he=0;
		
		MSADBHandler tempdb = MSADBHandler.open(act);		
		
		String maxtime = getmaxtimeindb(tempdb,"weight_table");
		Cursor cur = tempdb.rawQuery("select * from weight_table where time='"+maxtime+"'");
		maxtime = getmaxtimeindb(tempdb,"height_table");
		Cursor cur2 = tempdb.rawQuery("select * from height_table where time='"+maxtime+"'");
		
		if(cur.getCount() != 0 && cur2.getCount()!= 0) {			
			cur.moveToFirst();
			cur2.moveToFirst();
			we = cur.getDouble(1);
			he = cur2.getDouble(1);
			
			ContentValues newValues = new ContentValues();
			newValues.put("time", new java.util.Date().getTime());
			newValues.put("value", we/((he/100)*(he/100)));
			tempdb.insert("bmi_table", null, newValues);
		}		
		
		cur.close();
		cur2.close();
		tempdb.close();		
	}	
}